<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="UTF-8">
    <title>文件预览测试Demo</title>
    <script type="text/javascript" src="js/jquery-1.10.1.min.js"></script>
    <script type="text/javascript">
        //谷歌浏览器兼容showModalDialog开始
        var has_showModalDialog = !!window.showModalDialog;//定义一个全局变量判定是否有原生showModalDialog方法
        if(!has_showModalDialog &&!!(window.opener)){
            window.onbeforeunload=function(){
                window.opener.hasOpenWindow = false;        //弹窗关闭时告诉opener：它子窗口已经关闭
            }
        }
        //定义window.showModalDialog如果它不存在
        if(window.showModalDialog == undefined){
            window.showModalDialog = function(url,mixedVar,features){
                if(window.hasOpenWindow){
                    window.myNewWindow.focus();
                }
                window.hasOpenWindow = true;
                if(mixedVar) var mixedVar = mixedVar;
                if(features) var features = features.replace(/(dialog)|(px)/ig,"").replace(/;/g,',').replace(/\:/g,"=");
                var left = (window.screen.width - parseInt(features.match(/width[\s]*=[\s]*([\d]+)/i)[1]))/2;
                var top = (window.screen.height - parseInt(features.match(/height[\s]*=[\s]*([\d]+)/i)[1]))/2;
                window.myNewWindow = window.open(url,"_blank",features);
            }
        }
        //谷歌浏览器兼容showModalDialog结束


        function getRootPath(){
            //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
            var curWwwPath=window.document.location.href;
            //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
            var pathName=window.document.location.pathname;
            var pos=curWwwPath.indexOf(pathName);
            //获取主机地址，如： http://localhost:8083
            var localhostPaht=curWwwPath.substring(0,pos);
            //获取带"/"的项目名，如：/uimcardprj
            var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
            return(localhostPaht+projectName);
        }



        function uploadFile(type) {
            var fileObj= document.getElementById(type).files.item(0);
            var fileType=fileObj.name.substring(fileObj.name.lastIndexOf(".")+1)
            if(type=='wordToPdf' || type=="wordToHtml"){
                if("doc|docx".indexOf(fileType)==-1){
                    alert("请选择doc|docx 文件类型");
                    return;
                }

            }else if(type=="excelToHtml" || type=="excelToPdf"){
                if("xls|xlsx".indexOf(fileType)==-1){
                    alert("请选择xls|xlsx 文件类型")
                    return;
                }
            }
            var formFile = new FormData();
            formFile.append("file", fileObj); //加入文件对象
            $.ajax({
                async: false,
                type: "POST",           //因为是传输文件，所以必须是post
                data: formFile,
                processData: false,
                contentType: false,
                url: "uploadFile?type="+type,
                success:function(data){
                    data=JSON.parse(data);
                    if(data.resultStat == "SUCCESS"){
                        var url = data.realPath;
                        window.open(url);
                    }else if(data.resultStat=="ERROR"){
                        alert(data.msg);
                    }else{
                        alert("预览失败");
                    }

                }
            });

        }

    </script>
    <style type="text/css">
        tr{
            line-height:2
        }
    </style>
</head>
<body>

<form id="MainArea">

<table cellpadding="0" cellspacing="0" class="mainForm">
<tr>
    <td>word转pdf</td>
    <td>
        <input type="file" id="wordToPdf" />
    </td>
    <td>
        <button onclick="uploadFile('wordToPdf')">提交</button>
    </td>
</tr>
<tr>
    <td>word转html</td>
    <td>
        <input type="file"   id="wordToHtml"  />
    </td>
    <td>
        <button onclick="uploadFile('wordToHtml')">提交</button>
    </td>
</tr>
<tr>
    <td>excel转html</td>
    <td>
        <input type="file"  id="excelToHtml" />
    </td>
    <td>
        <button onclick="uploadFile('excelToHtml')">提交</button>
    </td>
</tr>

<tr>
    <td>excel转pdf</td>
    <td>
        <input type="file"  id="excelToPdf" />
    </td>
    <td>
        <button onclick="uploadFile('excelToPdf')">提交</button>
    </td>
</tr>



<%--    <tr>--%>
<%--        <td>text转html</td>--%>
<%--        <td>--%>
<%--            <input type="file"  id="textToHtml" />--%>
<%--        </td>--%>
<%--        <td>--%>
<%--            <button onclick="uploadFile('textToHtml')">提交</button>--%>
<%--        </td>--%>
<%--    </tr>--%>

</table>

</form>
</body>
</html>